<?php
class Accept_help extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		// 登录校验
		$this->common->check_login_mobile ();
		// 通过user_model的get_user_info_id方法获取个人信息
		$this->user_info = $this->user->get_user_info_id ( $_SESSION ['mobile'] ['id'] );
		
		$this->load->model ( 'order_model' );
	}
	
	/**
	 * 接受帮助
	 */
	public function index() {
		$this->load->helper ( 'form' );
		
		//$this->check_out_money ();
		
		$data ['left'] = $this->load->view ( 'mobile/left', '', TRUE );
		
		$data ['foot'] = $this->load->view ( 'mobile/foot', '', TRUE );
		
		$data ['user'] = $this->user_info;
		
		$data ['order_type'] = array (
				'静态钱包',
				'推广钱包' 
		);
		
		$data ['dakuan_type'] = array (
				'未打款',
				'已经打款',
				'拒绝打款' 
		);
		// echo $this->user_info['id'];
		$data ['log_list'] = $this->order_model->accept_order_log_list_uid ( $this->user_info ['id'] );
		
		$this->load->view ( 'mobile/accept_help', $data, FALSE );
	}
	
	/**
	 * 信息提交
	 */
	public function info_post() {
		if (empty ( $_POST ) or empty ( $_POST ['money'] )) {
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		
		$num = $this->order_model->get_order_shoukuan ( $this->user_info ['id'] );
		
		$accept = $this->order_model->accept_order_row_id ( $this->user_info ['id'] );
		
		if (! empty ( $num ) || ! empty ( $accept )) {
			exit ( $this->common->alert_msg_goback ( "还有尚未匹配的公益项目!" ) );
		}
		
		if ($_POST ['order_type'] == 0) {
			$s_money = 2000;
			// 1000倍数
			$mod_num = 1000;
			
			$max_money = $this->user_info ['jt_money'];
		} else {
			$s_money = 1000;
			// 500倍数
			$mod_num = 500;
			
			$max_money = $this->user_info ['tg_money'];
		}
		
		if ($_POST ['money'] > $max_money) {
			exit ( $this->common->alert_msg_goback ( '请输入正确金额' ) );
		}
		
		if ($_POST ['money'] < $s_money) {
			exit ( $this->common->alert_msg_goback ( '余额不足' ) );
		}
		
		if (! is_int ( $_POST ['money'] / $mod_num )) {
			exit ( $this->common->alert_msg_goback ( '请输入正确金额' ) );
		}
		
		// 新增获取订单
		$result = $this->order_model->accept_order_insert ( $this->user_info ['id'], $_POST ['order_type'], $_POST ['money'] );
		
		if ($result) {
			// 更新钱包金额
			$this->user->update_minus_user_money ( $_POST ['order_type'], $_POST ['money'], $this->user_info ['id'] );
			
			exit ( $this->common->alert_msg_url ( '提交成功', site_url ( 'mobile/accept_help' ) ) );
		} else {
			exit ( $this->common->alert_msg_goback ( '提交失败' ) );
		}
	}
	
	/**
	 * 获取得到各种利率的出场后的静态金额
	 */
	private function check_out_money() {
		// 获取自己未出场的记录
		$cc_list = $this->order_model->get_order_log_chuchang ( $this->user_info ['id'] );
		// var_dump($cc_list);
		if (! empty ( $cc_list )) {
			// 循环记录
			foreach ( $cc_list as $k => $v ) {
				// 判断是否存在
				$out_log = $this->order_model->get_out_log ( $v ['id'] );
				// 订单日志唯一
				if (! empty ( $out_log )) {
					continue;
				}
				
				// 付款日期+设定出场日期等于允许出场日期
				$out_time = $v ['dakuan_time'] + ($v ['out_day_num'] * 86400);
				
				// var_dump(date('Y-m-d H:i:s',$out_time));
				/*
				 * 出场时间达到，
				 * 匹配利率（0-6*1%）+
				 * 出场利率（按积分算等级/按等级算利率，4个等级，1-5天出场日期总和算利率）+
				 * 出场期额外等级利率中级0.5%、高级1%、顶级1.5%的每日收益
				 */
				if (time () >= $out_time) {
					$user_lv = $this->common->get_user_level ( $this->user_info ['score'] );
					// 匹配利率
					// $pipei_rate = $v['day_num'] * 1;
					$data ['pipei_rate'] = 0 * 1;
					// 出场利率
					$data ['out_rate'] = $user_lv ['first_day_rate'] + ($v ['out_day_num'] - 1) * 0.5;
					// echo $user_lv['first_day_rate'] ;
					// 额外等级利率
					$data ['level_rate'] = $user_lv ['lv_day_rate'] * $v ['out_day_num'];
					
					$data ['old_money'] = $v ['money'];
					
					$rate = ($data ['pipei_rate'] + $data ['out_rate'] + $data ['level_rate']) / 100;
					
					$data ['new_money'] = $v ['money'] + ($v ['money'] * $rate);
					
					$data ['ol_id'] = $v ['id'];
					
					$data ['user_id'] = $this->user_info ['id'];
					// 插入
					$this->order_model->out_log_insert ( $data );
					// 更新
					$this->order_model->order_log_update_chuchang ( $v ['id'] );
					// 更新全局防止多条记录覆盖
					$this->user_info ['jt_money'] += $data ['new_money'];
					// 更新
					$this->user->update_user_jt_money ( $this->user_info ['jt_money'], $this->user_info ['id'] );
					// 添加出场记录，更新订单日志状态，更新用户静态金额
				}
			}
		}
		// 未出场略过
	}
	
	/**
	 * 更新收款状态
	 */
	public function change_shoukuan_type($type, $id) {
		if (empty ( $type ) || empty ( $id )) {
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		
		$log = $this->order_model->get_order_log_id ( $id );
		
		// 24小时
		// $sy_time = 24 - floor((time() - $log['create_time'])/3600);
		
		// if($sy_time <= 0)
		// {
		// exit($this->common->alert_msg_goback('已过期'));
		// }
		// 更新打款状态
		$result = $this->order_model->update_order_log_shoukuan ( $type, $id );
		// var_dump($result);exit;
		
		if ($result) {
			if ($type == 1) {
				exit ( $this->common->alert_msg_url ( '确认收款', site_url ( 'mobile/accept_help' ) ) );
			} else {
				exit ( $this->common->alert_msg_url ( '未收到收款', site_url ( 'mobile/accept_help' ) ) );
			}
		} else {
			exit ( $this->common->alert_msg_url ( '更新失败', site_url ( 'mobile/accept_help' ) ) );
		}
	}
}